package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizSoundCardLog;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizSoundCardLogMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizSoundCardLogParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizSoundCardLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 声卡记录日志 服务实现类
 *
 * @author ssy
 * @date 2022/10/24 18:31
 */
@Service
public class BizSoundCardLogServiceImpl extends ServiceImpl<BizSoundCardLogMapper, BizSoundCardLog> implements BizSoundCardLogService {

    /**
     * 分页查询声卡记录日志
     * @param bizSoundCardLogParam
     * @return
     */
    @Override
    public PageResult<BizSoundCardLog> page(BizSoundCardLogParam bizSoundCardLogParam) {
        String[] list = new String[1];
        String gatewayIds = bizSoundCardLogParam.getGatewayIds();
        if(StringUtils.isNotBlank(gatewayIds)){
            list = gatewayIds.split(",");
        }else {
            list[0] = "";
        }
        // 查询分页结果
        return new PageResult<>(this.baseMapper.pageList(PageFactory.defaultPage(), bizSoundCardLogParam,list));
    }


    /**
     * 添加声卡记录日志
     * @param bizSoundCardLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizSoundCardLogParam bizSoundCardLogParam) {
        // 将dto转为实体
        BizSoundCardLog bizSoundCardLog = new BizSoundCardLog();
        BeanUtil.copyProperties(bizSoundCardLogParam, bizSoundCardLog);
        this.save(bizSoundCardLog);
    }

    /**
     * 删除声卡记录日志
     * @param bizSoundCardLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizSoundCardLogParam bizSoundCardLogParam) {
        this.removeById(bizSoundCardLogParam.getId());
    }

    /**
     * 编辑声卡记录日志
     * @param bizSoundCardLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizSoundCardLogParam bizSoundCardLogParam) {
        // 根据id查询实体
        BizSoundCardLog bizSoundCardLog = this.queryBizSoundCardLog(bizSoundCardLogParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizSoundCardLogParam, bizSoundCardLog);
        this.updateById(bizSoundCardLog);
    }

    /**
     * 查看详情声卡记录日志
     * @param bizSoundCardLogParam
     * @return
     */
    @Override
    public BizSoundCardLog detail(BizSoundCardLogParam bizSoundCardLogParam) {
        return this.queryBizSoundCardLog(bizSoundCardLogParam);
    }

    /**
     * 获取声卡记录日志
     *
     * @author ssy
     * @since 2022/10/24 18:31
     */
    private BizSoundCardLog queryBizSoundCardLog(BizSoundCardLogParam bizSoundCardLogParam) {
        return this.getById(bizSoundCardLogParam.getId());
    }
}
